package com.erps.sys.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.erps.sys.app.MyResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmkj.entity.ErspSysRole;

@Path("/erspsysrole")
public class ErspSysRoleRest {
	@POST
	// ��������ʽ
	@Path("/post")
	// ��������·��
	@Consumes("application/json")
	// ���ý�����ݸ�ʽ
	@Produces("application/json")
	// ���÷�����ݸ�ʽ
	public MyResponse post(JSONObject request) {
		MyResponse response = new MyResponse("OK");

		// ��ȡpersons����
		JSONArray persons;
		String sex;
		try {
			persons = request.getJSONArray("persons");
			sex = request.getString("sex");
		} catch (JSONException e) {
			e.printStackTrace();

			response.setMessage("ERROR");
			return response;
		}

		// ��ȡ��person��Ϣ
		int count = persons.length();
		Gson gson = new Gson();
		List<ErspSysRole> ps = gson.fromJson(persons.toString(), new TypeToken<List<ErspSysRole>>() {
		}.getType());
		for (int i = 0; i < count; i++) {
			ErspSysRole p = ps.get(i);
			System.out.println(p);
		}
		System.out.println(sex);
		return response;
	}

	@PUT
	@Path("/put")
	@Consumes("application/json")
	@Produces("application/json")
	public MyResponse put(JSONObject request) {
		MyResponse response = new MyResponse("OK");
		Gson gson = new Gson();
		ErspSysRole p = gson.fromJson(request.toString(), ErspSysRole.class);
		System.out.println(p);
		return response;
	}

	@DELETE
	@Path("/delete")
	@Produces("application/json")
	// ��URL��ѯ�����л�ȡ����
	public MyResponse delete(@QueryParam("name") List<String> name, @QueryParam("age") int age) {
		MyResponse response = new MyResponse("OK");
		System.out.println(name);
		System.out.println(age);
		return response;
	}

	@GET
	@Path("/{name}/get")
	@Produces("application/json")
	// ��URL·���л�ȡ����
	public MyResponse get(@PathParam("name") String name) {
		MyResponse response = new MyResponse("OK");
		System.out.println(name);
		return response;
	}
}